#include <pthread.h>
#include <sched.h>

#include <glog/logging.h>
#include <thread>

void bind_to_cpu(pthread_t thread, int cpu_id)
{
    cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    CPU_SET(cpu_id, &cpuset);
    pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
}

void* worker(void*)
{
    LOG(INFO) << std::this_thread::get_id();
    while (true) { /* 做一些计算 */
    }
    return nullptr;
}

int main(int argc, char* argv[])
{
    google::SetStderrLogging(google::LogSeverity(FLAGS_minloglevel));
    google::InitGoogleLogging(argv[0]);
    pthread_t t;
    pthread_create(&t, nullptr, worker, nullptr);
    bind_to_cpu(t, 2); // 绑定到 CPU 2
    pthread_join(t, nullptr);
    return 0;
}
